//
//  XMQRCodeView.m
//  XMJJ_zml
//
//  Created by 丁浩 on 2025/6/3.
//

#import "XMQRCodeView.h"

@interface XMQRCodeView ()

@property (nonatomic, strong) UIImageView *avatarImgView;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *userIdLabel;
@property (nonatomic, strong) UIImageView *qrcodeImgView;

@end

@implementation XMQRCodeView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self creatUI];
    }
    return self;
}

- (void)creatUI {
    [self addSubview:self.navView];
    
    [self addSubview:self.avatarImgView];
    [self addSubview:self.nameLabel];
    [self addSubview:self.userIdLabel];
    [self addSubview:self.qrcodeImgView];
}

- (void)setContentWithUserInfo:(XMUserInfoModel *)userInfoModel {
    if (userInfoModel.avatar && userInfoModel.avatar.length > 0) {
        [_avatarImgView sd_setImageWithURL:[NSURL URLWithString:userInfoModel.avatar]];
    }
    _nameLabel.text = userInfoModel.nickname;
    _userIdLabel.text = [NSString stringWithFormat:@"ID：%@", userInfoModel.idNo];
    
    UIImage *image = [self df_createNonInterpolatedUIImageFormCIImage:[self df_createQRcodeWithUrlstring:[NSString stringWithFormat:@"userId:%@", userInfoModel.objectId]] withSize:_qrcodeImgView.width];
    _qrcodeImgView.image = image;
}

- (CIImage *)df_createQRcodeWithUrlstring:(NSString *)urlString {
      // 1.实例化二维码滤镜
    CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    // 2.恢复滤镜的默认属性 (因为滤镜有可能保存上一次的属性)
    // 3.将字符串转换成NSdata
    NSData*data = [urlString dataUsingEncoding:NSUTF8StringEncoding];
    // 4.通过KVO设置滤镜, 传入data, 将来滤镜就知道要通过传入的数据生成二维码
    [filter setValue:data forKey:@"inputMessage"];
    // 5.生成二维码
    CIImage*outputImage = [filter outputImage];
    return outputImage;
}

- (UIImage *)df_createNonInterpolatedUIImageFormCIImage:(CIImage *)image withSize:(CGFloat)size {

    CGRect extent = CGRectIntegral(image.extent);
    CGFloat scale = MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent));
    // 1.创建bitmap;
    size_t width = CGRectGetWidth(extent) * scale;
    size_t height = CGRectGetHeight(extent) * scale;
    CGColorSpaceRef cs = CGColorSpaceCreateDeviceGray();
    CGContextRef bitmapRef = CGBitmapContextCreate(nil, width, height, 8, 0, cs, (CGBitmapInfo)kCGImageAlphaNone);
    CIContext *context = [CIContext contextWithOptions:nil];
    CGImageRef bitmapImage = [context createCGImage:image fromRect:extent];
    CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone);
    CGContextScaleCTM(bitmapRef, scale, scale);
    CGContextDrawImage(bitmapRef, extent, bitmapImage);
    // 2.保存bitmap到图片
    CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);
    CGContextRelease(bitmapRef);
    CGImageRelease(bitmapImage);
    return [UIImage imageWithCGImage:scaledImage];
}

#pragma mark - Init
- (UIImageView *)avatarImgView {
    if (!_avatarImgView) {
        _avatarImgView = [[UIImageView alloc] initWithFrame:CGRectMake(KScaleWidth(64.0), kNavagationBarH + KScaleWidth(95.0), KScaleWidth(60.0), KScaleWidth(60.0))];
        [_avatarImgView setCornerRadius:_avatarImgView.height/2];
    }
    return _avatarImgView;
}

- (UILabel *)nameLabel {
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.avatarImgView.maxX + KScaleWidth(10.0), _avatarImgView.y + KScaleWidth(2.0), self.width - _avatarImgView.maxX - KScaleWidth(10.0), KScaleWidth(32.0)) font:KFontSize(16.0) textColor:kColorHEX(0x333333)];
    }
    return _nameLabel;
}

- (UILabel *)userIdLabel {
    if (!_userIdLabel) {
        _userIdLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.nameLabel.x, _nameLabel.maxY, _nameLabel.width, KScaleWidth(20.0)) font:KFontSize(14.0) textColor:kColorHEX(0x666666)];
    }
    return _userIdLabel;
}

- (UIImageView *)qrcodeImgView {
    if (!_qrcodeImgView) {
        _qrcodeImgView = [[UIImageView alloc] initWithFrame:CGRectMake((self.width - KScaleWidth(242.0))/2, self.avatarImgView.maxY + KScaleWidth(32.0), KScaleWidth(242.0), KScaleWidth(237.0))];
    }
    return _qrcodeImgView;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
